package com.fengwk.cv4j.view.common;

import com.fengwk.cv4j.compute.Compute;
import com.fengwk.cv4j.factory.ViewFactory;
import com.fengwk.cv4j.view.View;
import com.fengwk.cv4j.view.ViewImpl;
import com.fengwk.support.exception.BaseException;

/**
 * 通用的pow算子
 * 
 * @author fengwk
 *
 * @param <T>
 */
public class PowView<T> extends ViewImpl<T> {
	
	private static final long serialVersionUID = -1806750571413781379L;
	
	public PowView(Compute<T> compute, ViewFactory<T> factory, @SuppressWarnings("unchecked") View<T>...vs) {
		super(compute, factory, vs);
		checkVs(2, vs);
	}
	
	@Override
	public T compute0() {
		return c().pow(t(0), t(1));
	}

	@Override
	public View<T> gradient0(int idx) {
	    // y=u^v, y'=v' * u^v * lnu + u' * u^(v-1) * v
		if (idx == 0)
			return f().mul(v(1), f().pow(v(0), f().sub(v(1), f().of(1)))); 
		if (idx == 1)
			return f().mul(f().pow(v(0), v(1)), f().log(v(0)));
		throw new BaseException("idx != 0 && idx != 1");
	}
	
	@Override
	public String toString() {
		return "pow";
	}

}
